﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.DataAccess;
using TaoPortal.Entity;


namespace TaoPortal.BusinessLogic.Repository
{
    public class SystemRepository : BaseRepository
    {
        public SystemRepository(DataContext dbcontext)
        {
            db = dbcontext;
            AutoCommit = true;
        }
        public SystemRepository()
            : this(new DataContext())
        {

        }

        public TP_AppClass GetClassByID(string id)
        {
            var data = db.TP_AppClass.Where(x => x.ID == id).FirstOrDefault();
            return data;
        }

        public void AddAppClass(TP_AppClass appClass)
        {
            db.TP_AppClass.Add(appClass);
            SaveChanges();
        }

        public void UpdateAppClass(TP_AppClass appClass)
        {
            var appc = db.TP_AppClass.Where(x => x.ID == appClass.ID).FirstOrDefault();
            appc.ClassName = appClass.ClassName;
            appc.ParentID = appClass.ParentID;
            appc.Sort = appClass.Sort;
            SaveChanges();
        }

        public void DeleteClass(string id)
        {
            db.Entry(new TP_AppClass { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            SaveChanges();
        }

        public void MoveClass(TP_AppClass appClass)
        {
            db.Entry(new TP_AppClass { ID = appClass.ID, ParentID = appClass.ParentID }).State = System.Data.Entity.EntityState.Deleted;
            SaveChanges();
        }

        public List<TP_AppClass> GetAppClass(string type)
        {
            var data = db.TP_AppClass.Where(x => x.ClassType == type).OrderBy(x => x.Sort).ToList();
            return data;
        }

        public List<TP_AppClass> GetAppClassByPID(string type, string pid)
        {
            var data = db.TP_AppClass.Where(x => x.ParentID == pid && x.ClassType == type).OrderBy(x => x.Sort).ToList();
            return data;
        }

        public List<TP_Menu> GetMenuList()
        {
            var data = db.TP_Menu.ToList();
            return data;
        }

        public List<TP_NavBar> GetNavBarList()
        {
            return db.TP_NavBar.OrderBy(x => x.Sort).ToList();
        }

        public TP_NavBar GetNavBarByID(string id)
        {
            return db.TP_NavBar.Where(x => x.ID == id).FirstOrDefault();
        }

        public void AddNavBar(TP_NavBar navBar)
        {
            db.TP_NavBar.Add(navBar);
            SaveChanges();
        }

        public void UpdateNavBar(TP_NavBar navBar)
        {
            db.UpdateEntity(new TP_NavBar
            {
                ID = navBar.ID,
                NavName = navBar.NavName,
                Status = navBar.Status,
                Sort = navBar.Sort,
                ParentID = navBar.ParentID,
                OpenType = navBar.OpenType,
                NavUrl = navBar.NavUrl,
                NavType = navBar.NavType
            });
            SaveChanges();
        }

        public void DeleteBavBar(string id)
        {
            db.Entry(new TP_NavBar { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            SaveChanges();
        }

        public bool HasLeafBavBar(string id)
        {
            return db.TP_NavBar.Where(x => x.ParentID == id).Count() > 0;
        }

        public void AddTraffic(TP_Traffic traffic)
        {
            db.TP_Traffic.Add(traffic);
            SaveChanges();
        }

        public PagedList<TP_Traffic> GetTrafficList(DateTime? createDate, int pageIndex, int pageSize)
        {
            var data = db.TP_Traffic.Where(x => x.CreateDate == createDate).OrderBy(x => x.CreateTime).ToPagedList(pageIndex, pageSize);
            return data;
        }

        public PagedList<TrafficStatistics> GetTrafficStatisticsList(DateTime? startDate, DateTime? endDate, int pageIndex, int pageSize)
        {
            var data = from t in db.TP_Traffic
                       select t;
            if (startDate != null)
                data = data.Where(x => x.CreateDate >= startDate);
            if (endDate != null)
                data = data.Where(x => x.CreateDate <= endDate);
            var list = (from t in data
                        group t by t.CreateDate into g
                        select new TrafficStatistics { CreateDate = g.Key, PV = g.Count() }).OrderBy(x => x.CreateDate).ToPagedList(pageIndex, pageSize);
            return list;
        }

        public void ClearTraffic(List<string> ids)
        {
            foreach (var id in ids)
            {
                db.Entry<TP_Traffic>(new TP_Traffic { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            }
            SaveChanges();
        }

        public void ClearTrafficByDate(List<DateTime> dates)
        {
            foreach (var date in dates)
            {
                var delObjs = db.TP_Traffic.Where(x => x.CreateDate == date).ToList();
                foreach (var item in delObjs)
                {
                    db.DeleteEntity(item);
                }
            }
            SaveChanges();
        }

        public PagedList<TP_SysLog> GetSysLogList(string logType, string Keyword, DateTime? startTime, DateTime? endTime, string user, int pageIndex, int pageSize)
        {
            var data = db.TP_SysLog.Select(x => x);
            if (!string.IsNullOrEmpty(logType))
                data = data.Where(x => x.LogType == logType);
            if (!string.IsNullOrEmpty(Keyword))
                data = data.Where(x => x.Msg.Contains(Keyword));
            if (startTime != null)
                data = data.Where(x => x.CreateTime >= startTime);
            if (endTime != null)
            {
                endTime = endTime.Value.AddDays(1);
                data = data.Where(x => x.CreateTime <= endTime);
            }
            if (!string.IsNullOrEmpty(user))
                data = data.Where(x => x.CurrentUser == user);
            return data.OrderBy(x => x.CreateTime).ToPagedList(pageIndex, pageSize);
        }

        public void DeleteSysLog(List<string> ids)
        {
            foreach (var id in ids)
            {
                db.Entry<TP_SysLog>(new TP_SysLog { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            }
            SaveChanges();
        }

        public PagedList<TP_SysMsg> GetSysMsgList(string msgType, string sender, string content, string parentId, DateTime? startTime, DateTime? endTime, int pageIndex, int pageSize)
        {
            var data = db.TP_SysMsg.Select(x => x);
            if (!string.IsNullOrEmpty(msgType))
                data = data.Where(x => x.MsgType == msgType);
            if (!string.IsNullOrEmpty(sender))
                data = data.Where(x => x.Sender == sender);
            if (!string.IsNullOrEmpty(content))
                data = data.Where(x => x.MsgBody.Contains(content));
            if (startTime != null)
                data = data.Where(x => x.CreateTime >= startTime);
            if (!string.IsNullOrEmpty(parentId))
                data = data.Where(x => x.ID == parentId || x.ParentID == parentId);
            else
                data = data.Where(x => x.ParentID == null);
            if (endTime != null)
            {
                endTime = endTime.Value.AddDays(1);
                data = data.Where(x => x.CreateTime <= endTime);
            }
            return data.OrderBy(x => x.CreateTime).ToPagedList(pageIndex, pageSize);
        }

        public void UpdateSysMsgStatus(string id)
        {
            db.UpdateProperty(new TP_SysMsg { ID = id, UnRead = true }, x => x.UnRead);
            SaveChanges();
        }

        public void DeleteSysMsg(List<string> ids)
        {
            foreach (var id in ids)
            {
                List<string> cids = db.TP_SysMsg.Where(x => x.ParentID == id).Select(x => x.ID).ToList();
                foreach (var cid in cids)
                {
                    db.Entry<TP_SysMsg>(new TP_SysMsg { ID = cid }).State = System.Data.Entity.EntityState.Deleted;
                }
                db.Entry<TP_SysMsg>(new TP_SysMsg { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            }
            SaveChanges();
        }

        public void AddSysMsg(TP_SysMsg msg)
        {
            db.TP_SysMsg.Add(msg);
            SaveChanges();
        }

        #region 添加广告

        public void AddAd(TP_SysAd ad)
        {
            db.TP_SysAd.Add(ad);
            SaveChanges();
        }

        public void UpdateAd(TP_SysAd ad)
        {
            db.Entry<TP_SysAd>(ad).State = System.Data.Entity.EntityState.Modified;
            SaveChanges();
        }

        public void DeleteAd(string id)
        {
            db.Entry<TP_SysAd>(new TP_SysAd { ID = id }).State = System.Data.Entity.EntityState.Deleted;
            SaveChanges();
        }

        public PagedList<TP_SysAd> GetAdList(string id, string name, int pageIndex, int pageSize)
        {
            var data = db.TP_SysAd.Select(x => x);
            if (!string.IsNullOrEmpty(id))
            {
                data = data.Where(x => x.ID == id);
            }
            if (!string.IsNullOrEmpty(name))
            {
                data = data.Where(x => x.AdName.Contains(name));
            }
            return data.OrderByDescending(x => x.AdType).ToPagedList(pageIndex, pageSize);
        }

        #endregion
    }
}
